package sample.algo.sistr.algosample.algos.arrays;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Arrays;

import sample.algo.sistr.algosample.R;
import sample.algo.sistr.algosample.algos.Algo;

public class Algo03   extends Algo {

    private TextView textViewTestResult;
    private EditText editText;


    public Algo03(Context context) {
        super("Predecessor", context);
        textViewTestResult = null;
        editText = null;
    }

    @Override
    public View GetDisplay() {
        ViewGroup root = (ViewGroup) ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.algo_array_03, null);

        textViewTestResult = (TextView)root.findViewById(R.id.textViewTest);

        Button buttonTest = (Button)root.findViewById(R.id.buttonTest);
        buttonTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuilder testResult = new StringBuilder();

                int res0 = function(18327);
                testResult.append("input[").append(18327).append("] return: ").append(res0).append("\n");

                if(textViewTestResult != null)
                    textViewTestResult.setText(testResult);
            }
        });

        editText = (EditText) root.findViewById(R.id.editText);
        editText.setText("1349");

        Button buttonRun = (Button)root.findViewById(R.id.buttonRun);
        buttonRun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuilder testResult = new StringBuilder();

                try {
                    int input = Integer.parseInt(editText.getText().toString());
                    testResult.append("input: ").append(input).append("\n");
                    int res = function(input);
                    testResult.append("result: ").append(res).append("\n");
                } catch(Exception e) {
                    testResult.append("Exception: ").append(e.getMessage());
                }
                if(textViewTestResult != null)
                    textViewTestResult.setText(testResult);
            }
        });

        return root;
    }

    private int function(int input) {

        // We right each digit one by one in an array
        int numberOfDigits = Integer.toString(input).length();

        int digits[] = new int[numberOfDigits];

        int k = input;
        for(int i = digits.length-1 ; i >= 0 ; i--){
            int tmp = k/10;
            digits[i] = k-tmp*10;
            k=tmp;
        }


        // We search for the first digit on the right side that have a smaller digit on the left part of the digits.
        int pivotIndex = -1;
        for(int i = digits.length-1 ; i >= 0 && pivotIndex == -1 ; i--){
            for(int j = i-1 ; j >= 0 ; j--){
                if(digits[i]<digits[j]){
                    // found it !
                    int tmp = digits[j];
                    digits[j] = digits[i];
                    digits[i] = tmp;

                    pivotIndex = j;
                }
            }
        }

        // We found no ways to re-arrange the digits to find a smaller number
        if(pivotIndex == -1)
            return input;

        // Now we have our pivot, we sort all the digits on the right side of that pivot (pivot not included) and sort them.
        Arrays.sort(digits, pivotIndex+1, digits.length);
        Reverse(digits, pivotIndex+1, digits.length);


        int res = 0;
        // Reconstruct the final number
        for(int i = 0 ; i < digits.length ; i++) {
            res *= 10;
            res += digits[i];
        }
        return res;
    }

    private void Reverse(int input[], int start, int end){

        int sp = start;
        int ep = end-1;
        int tmp;
        while(sp<ep){
            tmp = input[sp];
            input[sp++] = input[ep];
            input[ep--]=tmp;
        }

    }
}